﻿// a global variable

var users=[];
var zone;
var dbase

/* 
* Initializion point:
* 
* this function is called as soon as the extension
* is loaded in the server.
* 
* You can add here all the initialization code
* 
*/
function init()
{
	trace("Initing dbExtension")

	//初始化扩展的时候，将当前的zone的引用保存起来
	zone = _server.getCurrentZone();

	// get a reference to the database manager object
	// This will let you interact the database configure for this zone
	dbase = _server.getDatabaseManager()

}


/*
* This method is called by the server when an extension
* is being removed / destroyed.
* 
* Always make sure to release resources like setInterval(s)
* open files etc in this method.
* 
* In this case we delete the reference to the databaseManager
*/
function destroy()
{
	// Release the reference to the dbase manager
	delete dbase
}

/*
* 
* Handle Client Requests
* 
* cmd 		= a string with the client command to execute 
* params 	= list of parameters expected from the client
* user 		= the User object representing the sender
* fromRoom 	= the id of the room where the request was generated
* 
*/
function handleRequest(cmd, params, user, fromRoom)
{
	// 获取消息发送在房间里的 所有user
	users=zone.getRoom(fromRoom).getAllUsers();
	//根据不同的指令 进行不同的操作
	switch(cmd){
		case "getData":
			getData(cmd, params, user, fromRoom);
			break;
		case "bulletin_getdata":
			handleBulletinGetdata(params, user);//获取公告栏信息
			break;
		case "getOtherPersonInfo":
			getOtherPersonInfo(cmd, params, user, fromRoom);//获取其他用户的信息
			break;
		case "getUserInfo":
			getUserInfo(cmd, params, user, fromRoom);//人物加入房间时获取信息
			break;
		case "personalInfoChange":
			personalInfoChange(cmd, params, user, fromRoom);//修改个人信息
			break;
		case "updatePVcoins":
			var sql = "UPDATE users SET "
       		sql += "PVcoins=PVcoins+" +_server.escapeQuotes(params.PVCoins) +" where userNumber="+_server.escapeQuotes(params.userNumber);
			trace(sql);
			dbase.executeCommand(sql)
			break;
		case "updateOnlineTime":
			var sql = "UPDATE users SET "
       		sql += "onlineTime=onlineTime+" +_server.escapeQuotes(params.time) +" where userNumber="+_server.escapeQuotes(params.userNumber);
			trace(sql);
			dbase.executeCommand(sql)
			break;
		case "updateExp":
			var sql = "UPDATE users SET "
       		sql += "exp=exp+" +_server.escapeQuotes(params.exp) +" where userNumber="+_server.escapeQuotes(params.userNumber);
			trace(sql);
			dbase.executeCommand(sql)
			break;
		case "updateOfflineMessage":
			var sql = "UPDATE users SET "
       		sql += "offlineMessage=" +_server.escapeQuotes(params.mes) +" where userNumber="+_server.escapeQuotes(params.userNumber);
			trace(sql);
			dbase.executeCommand(sql)
			break;
		case "updateMessageBoard":
			var sql = "UPDATE users SET "
       		sql += "messageBoard=" +_server.escapeQuotes(params.mes) +" where userNumber="+_server.escapeQuotes(params.userNumber);
			trace(sql);
			dbase.executeCommand(sql)
			break;
		case "updateBuddyList":
			var sql = "UPDATE users SET "
       		sql += "buddyList=buddyList+" +_server.escapeQuotes(params.list) +" where userNumber="+_server.escapeQuotes(params.userNumber);
			trace(sql);
			dbase.executeCommand(sql)
			break;
		case "updateLastLocation":
			var sql = "UPDATE users SET "
       		sql += "lastMapId=" +_server.escapeQuotes(params.mapId)+",";
			sql += "lastX=" +_server.escapeQuotes(params.lastX)+",";
			sql += "lastY=" +_server.escapeQuotes(params.lastY)+" ";
			sql +="where userNumber="+_server.escapeQuotes(params.userNumber);
			trace(sql);
			dbase.executeCommand(sql)
			break;
	}

}


/*
* This method handles internal events
* Internal events are dispactched by the Zone or Room where the extension is attached to
* 
* the (evt) object
*/
function handleInternalEvent(evt)
{
	
}

function getData(cmd, params, user, fromRoom){
	
	var t1 = getTimer()
	
	var sql = "SELECT "+params.type+" FROM pvs.users where userNumber="+params.userNumber;		

	var queryRes = dbase.executeQuery(sql)
		
	// Show the time it took to parse the request
	var t2 = getTimer()
		
	trace("DB Request took: " + (t2-t1) + " mssssssss.")
		
		// prepare the response object
	var response = {}
		
	response._cmd = "getData"
		
		// Here we create an array for storing the database data
	if (queryRes != null)
	{
			var tempRow = queryRes.get(0)
				// From the record object we can get each field value
			response[params.type] = tempRow.getItem(params.type)
			response.type=params.type;

	}
	else
			trace("DB Query failed")
			
	_server.sendResponse(response, -1, null, [user]);
}

function handleBulletinGetdata(params,user){
	var response = new Object();
	response._cmd="bulletin_getdata";
	response.db=[];
	var nums = "SELECT COUNT(*) FROM bulletinboard" ;
	var getrecord_sql="select * from bulletinboard";
	var t1 = getTimer()
	var recordNums = dbase.executeQuery(nums);
	var records = dbase.executeQuery(getrecord_sql);
	var t2 = getTimer();
	trace("DB Request took: " + (t2-t1) + " ms....duprt");
	if (records != null)
		{
			for (var i = 0; i < records.size(); i++)
			{
				// Get a record
				var tempRow = records.get(i)
				
				// This object will hold the record data that we'll send to the client
				var item = {}

				// From the record object we can get each field value
				item.bulletinType = tempRow.getItem("bulletinType");
				item.date=tempRow.getItem("date");
				item.publisher=tempRow.getItem("publisher");
				item.tittle = tempRow.getItem("tittle");
				item.content = tempRow.getItem("content");

				response.db.push( item );
			}
		}
	_server.sendResponse(response, -1, null, [user]);
}

function getOtherPersonInfo(cmd, params, user, fromRoom){
		var t1 = getTimer()

		//trace("SELECT * FROM pvs.users where userNumber="+params.userNumber);
		var sql = "SELECT * FROM pvs.users where userNumber="+params.userNumber;
		var queryRes = dbase.executeQuery(sql)
		
		var t2 = getTimer()
		trace("DB Request took: " + (t2-t1) + " ms.")
		
		var response = {}
		response._cmd="getOtherPersonInfo"
		//response.db = []
		
		if (queryRes != null)
		{
			// Cycle through all records in the ResultSet
			//for (var i = 0; i < queryRes.size(); i++)
			//{
				var tempRow = queryRes.get(0)
				//var item = {}
				response.userNumber=tempRow.getItem("userNumber");
				response.username=tempRow.getItem("username");
				response.nickname = tempRow.getItem("nickname");
				response.level = tempRow.getItem("level");
				response.exp = tempRow.getItem("exp");
				response.PVcoins = tempRow.getItem("PVcoins");
				response.birthday = tempRow.getItem("birthday");
				response.qq= tempRow.getItem("qq");
				response.phoneNumber = tempRow.getItem("phoneNumber");
				response.province = tempRow.getItem("province");
				response.sex = tempRow.getItem("sex");
				response.department = tempRow.getItem("department");
				response.userClass = tempRow.getItem("class");
/*				response.pic = tempRow.getItem("pic");
				response.clothes = tempRow.getItem("clothes");
				response.association = tempRow.getItem("association");
				response.onlineTime = tempRow.getItem("onlineTime");
				response.buddyList = tempRow.getItem("buddyList");*/
				
				//response.db.push( item );
			//}
		}
		else
		{
			trace("DB Query failed");
		}
		_server.sendResponse(response, -1, null, [user],"xml");
}

function getUserInfo(cmd, params, user, fromRoom){
		
		var t1 = getTimer()

		trace("SELECT * FROM pvs.users where userNumber="+params.userNumber);
		var sql = "SELECT * FROM pvs.users where userNumber="+params.userNumber;
		var queryRes = dbase.executeQuery(sql)
		
		var t2 = getTimer()
		trace("DB Request took: " + (t2-t1) + " ms.")
		
		var response = {}
		response._cmd="getUserInfo"
		//response.db = []
		
		if (queryRes != null)
		{
			// Cycle through all records in the ResultSet
			//for (var i = 0; i < queryRes.size(); i++)
			//{
				var tempRow = queryRes.get(0)
				//var item = {}
				response.userNumber=tempRow.getItem("userNumber");
				response.username=tempRow.getItem("username");
				response.nickname = tempRow.getItem("nickname");
				response.level = tempRow.getItem("level");
				response.exp = tempRow.getItem("exp");
				response.PVcoins = tempRow.getItem("PVcoins");
				response.birthday = tempRow.getItem("birthday");
				response.qq= tempRow.getItem("qq");
				response.phoneNumber = tempRow.getItem("phoneNumber");
				response.province = tempRow.getItem("province");
				response.sex = tempRow.getItem("sex");
				response.department = tempRow.getItem("department");
				response.userClass = tempRow.getItem("class");
				response.pic = tempRow.getItem("pic");
				response.clothes = tempRow.getItem("clothes");
				response.association = tempRow.getItem("association");
				response.onlineTime = tempRow.getItem("onlineTime");
				response.buddyList = tempRow.getItem("buddyList");
				
				//response.db.push( item );
			//}
		}
		else
		{
			trace("DB Query failed");
		}
		_server.sendResponse(response, -1, null, [user],"xml");
}

function personalInfoChange(cmd, params, user, fromRoom){
		
		var t1 = getTimer()
		
		//var sql = "update users set qq="+params.qq+",phoneNumber="+params.phoneNumber+",province="+params.province+",birthday="+params.birthday+" where userNumber="+params.userNumber
		var sql = "UPDATE users SET "
        sql += " qq='" + _server.escapeQuotes(params.qq) + "',"
        sql += " phoneNumber='" + _server.escapeQuotes(params.phoneNumber) + "',"
        sql += " province='" + _server.escapeQuotes(params.province) + "',"
		sql += " birthday='" + _server.escapeQuotes(params.birthday) + "'"
        sql += " WHERE userNumber='" + params.userNumber + "'"

		
		trace(sql);
		var queryRes = dbase.executeCommand(sql)
		
		var t2 = getTimer()
		trace("DB Request took: " + (t2-t1) + " ms.")
}